package me.jessyan.armscomponent.commonres.widget.wheelview;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import me.jessyan.armscomponent.commonres.R;


/**
 * @Author: xuwei
 * @Date: 2020/8/25 15:10
 * @Description: 月份选择弹窗
 */
public class MonthPickerPopWindow extends PopupWindow implements View.OnClickListener {
    WheelView wheelMonth;
    WheelView wheelYear;
    private List<Integer> mData = new ArrayList<>();
    private OnConfirmListener mListener;
    private int mSelectData;
    private String currentMonth;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    private TimePickerView timePickerView;
    private Context context;

    public void setOnConfirmListener(OnConfirmListener listener) {
        this.mListener = listener;
    }

    public MonthPickerPopWindow(Context context, String currentMonth) {
        super(context);
        this.currentMonth = currentMonth;
        this.context = context;
        initView(context);
        initData();
    }

    private void initView(Context context) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.month_picker_pop_layout, null);
        setContentView(rootView);
        wheelMonth = rootView.findViewById(R.id.wheel_month);
        wheelYear = rootView.findViewById(R.id.wheel_year);
        wheelYear.setCyclic(false);
        wheelMonth.setCyclic(false);
        rootView.findViewById(R.id.tv_confirm).setOnClickListener(this);
        rootView.findViewById(R.id.tv_cancel).setOnClickListener(this);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);
        this.setAnimationStyle(R.style.popWindow_anim_from_bottom);
        this.setBackgroundDrawable(null); //去掉popupWindow边框
        this.setOutsideTouchable(false);
    }

    private void initData() {
        try {
            Date date = sdf.parse(currentMonth);
            Calendar startDate = Calendar.getInstance();
            startDate.setTime(date);
//            startDate.set(date.getYear(), date.getMonth());

            Calendar endDate = Calendar.getInstance();
            endDate.setTime(date);
            endDate.set(Calendar.MONTH, -12);
            int startYear = startDate.get(Calendar.YEAR);
            List<Integer> yearList = new ArrayList<>();
            yearList.add(startYear);
            int endYear = endDate.get(Calendar.YEAR);
            if (startYear != endYear) {
                yearList.add(endYear);
            }
            //noinspection unchecked
            WheelAdapter<Integer> mYearAdapter = new ArrayWheelAdapter<Integer>(yearList);
            wheelYear.setAdapter(mYearAdapter);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        mData.add(1);
        mData.add(2);
        mData.add(3);
        mData.add(4);
        mData.add(5);
        mData.add(6);
        mData.add(7);
        mData.add(8);
        mData.add(9);
        mData.add(10);
        mData.add(11);
        mData.add(12);
        WheelAdapter<Integer> mMonthAdapter = new ArrayWheelAdapter<Integer>(mData);
        wheelMonth.setAdapter(mMonthAdapter);
        wheelMonth.setOnItemSelectedListener(index -> mSelectData = mData.get(index));
    }

    private void initLunarPicker() {
//        Calendar selectedDate = Calendar.getInstance();//系统当前时间
//        Calendar startDate = Calendar.getInstance();
//        startDate.set(2014, 1, 23);
//        Calendar endDate = Calendar.getInstance();
//        endDate.set(2069, 2, 28);
        //时间选择器 ，自定义布局

        Date date;
        try {
            date = sdf.parse(currentMonth);
            Calendar startDate = Calendar.getInstance();
            startDate.setTime(date);
//            startDate.set(date.getYear(), date.getMonth());

            Calendar endDate = Calendar.getInstance();
            endDate.setTime(date);
            endDate.set(Calendar.MONTH, -12);

            timePickerView = new TimePickerBuilder(context, (date1, v) -> {

            }).setRangDate(startDate, endDate)
                    .setLayoutRes(R.layout.custom_time_pickerview, v -> {
                        final TextView tvSubmit = v.findViewById(R.id.tv_confirm);
                        TextView tvCancel = v.findViewById(R.id.tv_cancel);
//                            TextView tvSelect = v.findViewById(R.id.tv_select);
                        tvSubmit.setOnClickListener(v12 -> {
                            timePickerView.returnData();
                            timePickerView.dismiss();
                        });
                        tvCancel.setOnClickListener(v1 -> timePickerView.dismiss());
                    })
                    .setType(new boolean[]{true, true, false, false, false, false})
                    .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                    .setDividerColor(Color.RED)
                    .build();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_cancel) {
            dismiss();
        } else if(view.getId() ==R.id.tv_confirm){
            dismiss();
            if (mListener != null) {
                mListener.onConfirmClick(mSelectData);
            }
        }
    }

    public interface OnConfirmListener {
        void onConfirmClick(int data);
    }
}
